import 'package:flutter/material.dart';

import 'package:get/get.dart';

import '../controllers/proxy_setting_controller.dart';

class ProxySettingView extends GetView<ProxySettingController> {
  const ProxySettingView({super.key});

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(ProxySettingController());

    return Scaffold(
      appBar: AppBar(title: const Text('ProxySetting'), centerTitle: true),
      // body: Container(
      //   margin: EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 8.0),
      //   width: double.infinity,
      //   child: SingleChildScrollView(
      //     child: Column(
      //       crossAxisAlignment: CrossAxisAlignment.stretch,
      //       children: [
      //         _buildProxyEnableCheckBox(),
      //         SizedBox(height: 16),
      //         _buildAddressInputText(),
      //       ],
      //     ),
      //   ),
      // ),
      body: LayoutBuilder(
        builder: (context, constraints) {
          return SingleChildScrollView(
            child: Container(
              margin: EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 8.0),
              width: double.infinity,
              constraints: BoxConstraints(minHeight: constraints.maxHeight),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  _buildProxyEnableCheckBox(),
                  SizedBox(height: 16),
                  _buildAddressInputText(),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  Widget _buildProxyEnableCheckBox() {
    return Obx(
      () => CheckboxListTile(
        title: Text('是否开启代理'),
        value: controller.enableProxy.value,
        controlAffinity: ListTileControlAffinity.leading,
        onChanged: (bool? value) {
          controller.onEnableProxyChange(value);
        },
      ),
    );
  }

  Widget _buildAddressInputText() {
    return Obx(
      () => AnimatedSize(
        duration: Duration(milliseconds: 500),
        // height: controller.enableProxy.value ? 160 : 0,
        // width: double.infinity,
        // child: Visibility(
        //   visible: controller.enableProxy.value,
        //   child: Column(
        //     // mainAxisSize: MainAxisSize.min,
        //     crossAxisAlignment: CrossAxisAlignment.stretch,
        //     children: [
        //       TextField(
        //         onChanged: (text) {
        //           controller.updateAddressInputText(text);
        //         },
        //         controller: controller.addressEditController,
        //         keyboardType: TextInputType.text,
        //         decoration: InputDecoration(
        //           hintText: '请输入代理地址',
        //           border: OutlineInputBorder(),
        //           suffixIcon: IconButton(
        //             onPressed: () {
        //               controller.cleanAddressInputText();
        //             },
        //             icon: Icon(Icons.close),
        //           ),
        //         ),
        //       ),
        //       SizedBox(height: 16.0),
        //       ElevatedButton(
        //         onPressed: () {
        //           final text = controller.addressEditController.text;
        //           controller.saveAddress(text);
        //         },
        //         // style: ElevatedButton.styleFrom(
        //         //   minimumSize: Size(double.infinity, 36.0),
        //         // ),
        //         child: Text('保存'),
        //       ),
        //     ],
        //   ),
        // ),
        child:
            controller.enableProxy.value
                ? Column(
                  // mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    TextField(
                      onChanged: (text) {
                        controller.updateAddressInputText(text);
                      },
                      controller: controller.addressEditController,
                      keyboardType: TextInputType.text,
                      decoration: InputDecoration(
                        hintText: '请输入代理地址',
                        border: OutlineInputBorder(),
                        suffixIcon: IconButton(
                          onPressed: () {
                            controller.cleanAddressInputText();
                          },
                          icon: Icon(Icons.close),
                        ),
                      ),
                    ),
                    SizedBox(height: 16.0),
                    ElevatedButton(
                      onPressed: () {
                        final text = controller.addressEditController.text;
                        controller.saveAddress(text);
                      },
                      // style: ElevatedButton.styleFrom(
                      //   minimumSize: Size(double.infinity, 36.0),
                      // ),
                      child: Text('保存'),
                    ),
                  ],
                )
                : SizedBox.shrink(),
      ),
    );
  }
}
